package com.pdd.controller;

import com.pdd.beans.Comment;
import com.pdd.dto.DTO;
import com.pdd.service.CommentService;
import com.pdd.vo.comment.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论控制器
 */
@Api(description = "评论接口",tags = {"评论接口"})
@CrossOrigin(origins = { "*" })
@Controller
public class CommentController {

    @Autowired
    @Setter
    private CommentService commentService;

    @ApiOperation(value = "添加评论信息")
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    @ResponseBody
    public DTO addComment(CommentVO vo){
        // 1. 封装参数
        Comment comment = new Comment();
        comment.setU_id(vo.getU_id());
        comment.setP_id(vo.getP_id());
        comment.setContent(vo.getContent());

        // 2. 执行新增
        boolean result = commentService.addComment(comment);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }
    @ApiOperation(value = "删除评论")
    @RequestMapping(value = "/comment/{c_id}", method = RequestMethod.DELETE)
    public DTO delComment(@PathVariable(value = "c_id") Integer c_id){

        boolean result = commentService.delComment(c_id);
            if (result){
            return DTO.success();
        }else {
            return DTO.error();
        }
    }

    @ApiOperation(value = "更新评论信息")
    @RequestMapping(value = "/comment", method = RequestMethod.PUT)
    @ResponseBody
    public DTO uplComment(CommentVO vo){
        // 1. 封装参数
        Comment comment = new Comment();
        comment.setU_id(vo.getU_id());
        comment.setP_id(vo.getP_id());
        comment.setContent(vo.getContent());
        comment.setTime(vo.getTime());

        // 2. 执行更新
        boolean result = commentService.uplComment(comment);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }

    @ApiOperation(value = "查询评论信息")
    @RequestMapping(value = "/comment", method = RequestMethod.GET)
    @ResponseBody
    public DTO getComment(){
        // 1. 执行查询操作
        List<Comment> list = commentService.getComment();

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
    @ApiOperation(value = "根据商品ID查询评论信息")
    @RequestMapping(value = "/comment/{p_id}", method = RequestMethod.GET)
    @ResponseBody
    public DTO getById(@PathVariable(value = "p_id") Integer p_id){
        // 1. 执行查询操作
        Comment list = commentService.getById(p_id);

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
}
